package org.lyg.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import org.lyg.dao.IBlogMapper;
import org.lyg.domain.Blog;
import org.lyg.domain.BlogCondition;
import org.lyg.service.IBlogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("blogService")
public class BlogServiceImpl implements IBlogService {
    @Autowired
    public IBlogMapper blogMapper;

    @Override
    public PageInfo<Blog> list(int size, int page, BlogCondition condition) {
        PageHelper.startPage(page, size);
        List<Blog> blogList = blogMapper.list(condition);
        return new PageInfo<>(blogList);
    }

    @Override
    public Blog getBlogById(int id) {
        return blogMapper.getBlogById(id);
    }

    @Override
    public PageInfo<Blog> getBlogListByUserId(int id, int page, int size) {
        PageHelper.startPage(page, size);
        List<Blog> blogList = blogMapper.getBlogByUserId(id);
        return new PageInfo<>(blogList);
    }

    @Override
    public int getCountByUserId(int id) {
        return blogMapper.getCountByUserId(id);
    }

    @Override
    public boolean update(Blog blog) {
        return blogMapper.update(blog) > 0;
    }

    @Override
    public boolean save(Blog blog) {
        System.out.println(blog);
        int result;
        if(blogMapper.getBlogById(blog.getId())==null){
            result=blogMapper.save(blog);
        }else{
            result=blogMapper.update(blog);
        }
        return result > 0;
    }

    @Override
    public void del(int id) {
        blogMapper.del(id);
    }
}
